import ajax from '../libs/ajax.js';
import tools from '../libs/tools.js';

//#region 留言板信息获取
let txtqtitle = document.getElementById('txtqtitle');
let btnquery = document.getElementById('btnquery');

//#region 排序方式
let a = document.getElementById('a');
let b = document.getElementById('b');
let c = document.getElementById('c');
let i = 5;
a.addEventListener('click', () => {
  location.href = './twtwz.html';
});
b.addEventListener('click', () => {
  location.href = './twtwz01.html';
});
c.addEventListener('click', () => {
  location.href = './twtwz02.html';
});
//#endregion
let list = {
  info: '',
  username: '',
  orderBy: i
};
console.log('zzz', list.orderBy);

let page = {
  pageNumber: 1,
  pageSize: 4
};

let nr1 = [];

function hutao() {
  list.info = txtqtitle.value;
  let cs = tools.concatJson(list, page);
  ajax.send('/message/queryAll', cs, (data) => {
    if (data.success) {
      nr1 = data.list;
      page = data.page;
      zs();
      fy();
    }
  });
}

function zs() {
  for (let i = 0; i < nr1.length; i++) {
    const x = nr1[i];
    if (i == 0) {
      if (!x.userInfo.img == '') {
        let imgLogo1 = document.getElementById('imgLogo1');
        imgLogo1.src = `${x.userInfo.img}`;

        //账号
        let zh1 = document.getElementById('zh1');
        zh1.innerHTML = x.user.nickname;

        //时间
        let sj1 = document.getElementById('sj1');
        sj1.innerHTML = tools.FormData(x.lastupdate);

        //标题
        let bt1 = document.getElementById('bt1');
        let btt1 = document.getElementById('btt1');
        btt1.value = x.umid;
        bt1.innerHTML = x.title;

        //点击量
        let dj1 = document.getElementById('dj1');
        dj1.innerHTML = '<i class="iconfont">&#xe6ce;</i>' + x.hits + '点击量';

        //点赞
        let dianzhan1 = document.getElementById('dianzhan1');
        dianzhan1.innerHTML =
          '<i class="iconfont">&#xe8ab;</i>' + x.praiseCount + '点赞';
        dianzhan1.addEventListener('click', () => {
          let css = {
            umid: x.umid
          };
          ajax.send('/message/support', css, (data) => {
            if (data.message) {
              showToast(data.message);
              hutao();
            }
          });
        });
        //评论
        let pingr1 = document.getElementById('pingr1');
        pingr1.innerHTML =
          '<i class="iconfont">&#xe6ad;</i>' + x.replyCount + '评论';
      }

      //账号
      let zh1 = document.getElementById('zh1');
      zh1.innerHTML = x.user.nickname;

      //时间
      let sj1 = document.getElementById('sj1');
      sj1.innerHTML = tools.FormData(x.lastupdate);

      //标题
      let bt1 = document.getElementById('bt1');
      let btt1 = document.getElementById('btt1');
      btt1.value = x.umid;
      bt1.innerHTML = x.title;

      //点击量
      let dj1 = document.getElementById('dj1');
      dj1.innerHTML = '<i class="iconfont">&#xe6ce;</i>' + x.hits + '点击量';
      //点赞
      let dianzhan1 = document.getElementById('dianzhan1');
      dianzhan1.innerHTML =
        '<i class="iconfont">&#xe8ab;</i>' + x.praiseCount + '点赞';
      dianzhan1.addEventListener('click', () => {
        let css = {
          umid: x.umid
        };
        ajax.send('/message/support', css, (data) => {
          if (data.message) {
            showToast(data.message);
            hutao();
          }
        });
      });

      //评论
      let pingr1 = document.getElementById('pingr1');
      pingr1.innerHTML =
        '<i class="iconfont">&#xe6ad;</i>' + x.replyCount + '评论';
    }
    if (i == 1) {
      if (!x.userInfo.img == '') {
        //图片
        let imgLogo2 = document.getElementById('imgLogo2');
        imgLogo2.src = `${x.userInfo.img}`;

        let zh2 = document.getElementById('zh2');
        zh2.innerHTML = x.user.nickname;

        let sj2 = document.getElementById('sj2');
        sj2.innerHTML = tools.FormData(x.lastupdate);

        let bt2 = document.getElementById('bt2');
        let btt2 = document.getElementById('btt2');
        btt2.value = x.umid;
        bt2.innerHTML = x.title;

        //点击量
        let dj2 = document.getElementById('dj2');
        dj2.innerHTML = '<i class="iconfont">&#xe6ce;</i>' + x.hits + '点击量';
        //点赞
        let dianzhan2 = document.getElementById('dianzhan2');
        dianzhan2.innerHTML =
          '<i class="iconfont">&#xe8ab;</i>' + x.praiseCount + '点赞';
        dianzhan2.addEventListener('click', () => {
          let css = {
            umid: x.umid
          };
          ajax.send('/message/support', css, (data) => {
            if (data.message) {
              showToast(data.message);
              hutao();
            }
          });
        });

        //评论
        let pingr2 = document.getElementById('pingr2');
        pingr2.innerHTML =
          '<i class="iconfont">&#xe6ad;</i>' + x.replyCount + '评论';
      }
      let zh2 = document.getElementById('zh2');
      zh2.innerHTML = x.user.nickname;

      let sj2 = document.getElementById('sj2');
      sj2.innerHTML = tools.FormData(x.lastupdate);

      let bt2 = document.getElementById('bt2');
      let btt2 = document.getElementById('btt2');
      btt2.value = x.umid;
      bt2.innerHTML = x.title;
      //点击量
      let dj2 = document.getElementById('dj2');
      dj2.innerHTML = '<i class="iconfont">&#xe6ce;</i>' + x.hits + '点击量';
      //点赞
      let dianzhan2 = document.getElementById('dianzhan2');
      dianzhan2.innerHTML =
        '<i class="iconfont">&#xe8ab;</i>' + x.praiseCount + '点赞';
      dianzhan2.addEventListener('click', () => {
        let css = {
          umid: x.umid
        };
        ajax.send('/message/support', css, (data) => {
          if (data.message) {
            showToast(data.message);
            hutao();
          }
        });
      });
      //评论
      let pingr2 = document.getElementById('pingr2');
      pingr2.innerHTML =
        '<i class="iconfont">&#xe6ad;</i>' + x.replyCount + '评论';
    }
    if (i == 2) {
      if (!x.userInfo.img == '') {
        //图片
        let imgLogo3 = document.getElementById('imgLogo3');
        imgLogo3.src = `${x.userInfo.img}`;

        let zh3 = document.getElementById('zh3');
        zh3.innerHTML = x.user.nickname;

        let sj3 = document.getElementById('sj3');
        sj3.innerHTML = tools.FormData(x.lastupdate);

        let bt3 = document.getElementById('bt3');
        let btt3 = document.getElementById('btt3');
        btt3.value = x.umid;
        bt3.innerHTML = x.title;
        //点击量
        let dj3 = document.getElementById('dj3');
        dj3.innerHTML = '<i class="iconfont">&#xe6ce;</i>' + x.hits + '点击量';

        //点赞
        let dianzhan3 = document.getElementById('dianzhan3');
        dianzhan3.innerHTML =
          '<i class="iconfont">&#xe8ab;</i>' + x.praiseCount + '点赞';
        dianzhan3.addEventListener('click', () => {
          let css = {
            umid: x.umid
          };
          ajax.send('/message/support', css, (data) => {
            if (data.message) {
              showToast(data.message);
              hutao();
            }
          });
        });

        //评论
        let pingr3 = document.getElementById('pingr3');
        pingr3.innerHTML =
          '<i class="iconfont">&#xe6ad;</i>' + x.replyCount + '评论';
      }
      let zh3 = document.getElementById('zh3');
      zh3.innerHTML = x.user.nickname;

      let sj3 = document.getElementById('sj3');
      sj3.innerHTML = tools.FormData(x.lastupdate);

      let bt3 = document.getElementById('bt3');
      let btt3 = document.getElementById('btt3');
      btt3.value = x.umid;
      bt3.innerHTML = x.title;
      //点击量
      let dj3 = document.getElementById('dj3');
      dj3.innerHTML = '<i class="iconfont">&#xe6ce;</i>' + x.hits + '点击量';
      //点赞
      let dianzhan3 = document.getElementById('dianzhan3');
      dianzhan3.innerHTML =
        '<i class="iconfont">&#xe8ab;</i>' + x.praiseCount + '点赞';
      dianzhan3.addEventListener('click', () => {
        let css = {
          umid: x.umid
        };
        ajax.send('/message/support', css, (data) => {
          if (data.message) {
            showToast(data.message);
            hutao();
          }
        });
      });
      //评论
      let pingr3 = document.getElementById('pingr3');
      pingr3.innerHTML =
        '<i class="iconfont">&#xe6ad;</i>' + x.replyCount + '评论';
    }
    if (i == 3) {
      if (!x.userInfo.img == '') {
        //图片
        let imgLogo4 = document.getElementById('imgLogo4');
        imgLogo4.src = `${x.userInfo.img}`;

        let zh4 = document.getElementById('zh4');
        zh4.innerHTML = x.user.nickname;

        let sj4 = document.getElementById('sj4');
        sj4.innerHTML = tools.FormData(x.lastupdate);
        let bt4 = document.getElementById('bt4');
        let btt4 = document.getElementById('btt4');
        btt4.value = x.umid;
        bt4.innerHTML = x.title;

        //点击量
        let dj4 = document.getElementById('dj4');
        dj4.innerHTML = '<i class="iconfont">&#xe6ce;</i>' + x.hits + '点击量';

        //点赞
        let dianzhan4 = document.getElementById('dianzhan4');
        dianzhan4.innerHTML =
          '<i class="iconfont">&#xe8ab;</i>' + x.praiseCount + '点赞';
        dianzhan4.addEventListener('click', () => {
          let css = {
            umid: x.umid
          };
          ajax.send('/message/support', css, (data) => {
            if (data.message) {
              showToast(data.message);
              hutao();
            }
          });
        });

        //评论
        let pingr4 = document.getElementById('pingr4');
        pingr4.innerHTML =
          '<i class="iconfont">&#xe6ad;</i>' + x.replyCount + '评论';
      }
      let zh4 = document.getElementById('zh4');
      zh4.innerHTML = x.user.nickname;

      let sj4 = document.getElementById('sj4');
      sj4.innerHTML = tools.FormData(x.lastupdate);
      let bt4 = document.getElementById('bt4');
      let btt4 = document.getElementById('btt4');
      btt4.value = x.umid;
      bt4.innerHTML = x.title;

      //点击量
      let dj4 = document.getElementById('dj4');
      dj4.innerHTML = '<i class="iconfont">&#xe6ce;</i>' + x.hits + '点击量';

      //点赞
      let dianzhan4 = document.getElementById('dianzhan4');

      dianzhan4.innerHTML =
        '<i class="iconfont">&#xe8ab;</i>' + x.praiseCount + '点赞';
      dianzhan4.addEventListener('click', () => {
        let css = {
          umid: x.umid
        };
        ajax.send('/message/support', css, (data) => {
          if (data.message) {
            showToast(data.message);
            hutao();
          }
        });
      });

      //评论
      let pingr4 = document.getElementById('pingr4');
      pingr4.innerHTML =
        '<i class="iconfont">&#xe6ad;</i>' + x.replyCount + '评论';
    }
  }
}

//#endregion

//#region 获取用户信息
let tbUser = {};
let tbUserInfo = {};
let userOtherInfo = {};

function queryUserInfo() {
  ajax.send('/user/auth/getUserInfo', {}, (data) => {
    if (data.success) {
      tbUser = data.tbUser;
      tbUserInfo = data.tbUserInfo;
      userOtherInfo = data.userOtherInfo;
      showUser();
    } else {
      showToast('用户未登录');
    }
  });
}
//#endregion

//#region 点击图片上传
let imgLogo5 = document.getElementById('imgLogo5');
imgLogo5.addEventListener('click', () => {
  //浏览文件
  tools.openFile((file) => {
    if (!file) {
      return;
    }
    //上传图像
    ajax.file(file, '用户图像', (data) => {
      if (!data.success) {
        return;
      }

      //如果原始头像也是上传的文件，要删除，避免上传图片越来越多
      if (ajax.isFileurl(tbUserInfo.img)) {
        let fid = ajax.geturlfid(tbUserInfo.img);

        ajax.send('/user/file/delete', { fid: fid }, () => {});
      }

      //获取上传文件url
      let url = ajax.getFileUrl(data.data.fid);
      //修改用户的头像地址信息
      tbUserInfo.img = url;
      let info = JSON.parse(JSON.stringify(tbUserInfo));
      info.nickname = tbUser.nickname;

      ajax.send('/user/auth/updateUserInfo', info, () => {
        queryUserInfo();
        hutao();
      });
    });
  });
});
//#endregion

//#region 展示用户信息
function showUser() {
  // 处理用户头像
  if (tbUserInfo.img != '') {
    imgLogo.setAttribute('src', tbUserInfo.img);
    imgLogo5.setAttribute('src', tbUserInfo.img);
  }
}
//#endregion

//#region =========分页============
let fenye1 = document.querySelectorAll('nav>a');
let fenye2 = document.querySelector('nav>span');
function fy() {
  fenye2.innerHTML = `
  记录数/当前页/总页数
  ${page.total}/${page.pageNumber}/${page.pageCount}
  `;
}

fenye1[0].addEventListener('click', () => {
  if (page.pageNumber == 1) {
    return;
  }
  page.pageNumber = 1;
  hutao();
});
fenye1[1].addEventListener('click', () => {
  page.pageNumber--;
  if (page.pageNumber < 1) {
    page.pageNumber = 1;
    return;
  }
  hutao();
});
fenye1[2].addEventListener('click', () => {
  page.pageNumber++;
  if (page.pageNumber > page.pageCount) {
    page.pageNumber = page.pageCount;
    return;
  }
  hutao();
});
fenye1[3].addEventListener('click', () => {
  if (page.pageNumber == page.pageCount) {
    return;
  }
  page.pageNumber = page.pageCount;
  hutao();
});

//#endregion

//#region 搜索
btnquery.addEventListener('click', () => {
  page.pageNumber = 1;
  hutao();
});
//#endregion

//#region 安全退出
let spExit = document.getElementById('spExit');

spExit.addEventListener('click', () => {
  ajax.send('/user/auth/logout', {}, () => {
    location.href = './twt.html';
  });
});

//#endregion

//#region 进入个人页面
let imgLogo = document.getElementById('imgLogo');
imgLogo.addEventListener('click', () => {
  location.href = './zy.html';
});

//#endregion

//#region 公用的轻提示对话框

let liveToast = document.getElementById('liveToast');
let liveToastMessing = document.querySelector('#liveToast .toast-body');

const toast = new bootstrap.Toast(liveToast);

function showToast(message) {
  liveToastMessing.innerHTML = message;
  toast.show();
}
//#endregion

//#region 发布留言
let kscz = document.getElementById('kscz');
kscz.addEventListener('click', () => {
  location.href = './pr.html';
});
//#endregion

//#region 公告信息查询
let gg1 = document.getElementById('gg1');
let gg2 = document.getElementById('gg2');
let gg3 = document.getElementById('gg3');
 let sz = [];
function glliuy() {
  let cs2 = {
    pageNumber: 1,
    pageSize: 3
  };
  ajax.send('/message/queryNotice', cs2, (data) => {
    if (data.success) {
      sz = data.list;
      cs2 = data.page;
      showdata();
    }
  });
}

function showdata() {
  for (let i = 0; i < sz.length; i++) {
    console.log('zzz',sz[i])
    const t = sz[i];
    gg1.innerHTML=sz[0].title;
    gg1.setAttribute('data-bs-toggle', 'modal');
    gg1.setAttribute('data-bs-target', '#ggDialog');
    gg1.addEventListener('click',()=>{
      let ggnr1=document.getElementById('ggnr1');
      ggnr1.innerHTML=sz[0].info;
    })

    gg2.innerHTML=sz[1].title;
    gg2.setAttribute('data-bs-toggle', 'modal');
    gg2.setAttribute('data-bs-target', '#ggDialog');
    gg2.addEventListener('click',()=>{
      let ggnr1=document.getElementById('ggnr1');
      ggnr1.innerHTML=sz[1].info;
    })
    gg3.innerHTML=sz[2].title;
    gg3.setAttribute('data-bs-toggle', 'modal');
    gg3.setAttribute('data-bs-target', '#ggDialog');
    gg3.addEventListener('click',()=>{
      let ggnr1=document.getElementById('ggnr1');
      ggnr1.innerHTML=sz[2].info;
    })
  }
}
//#endregion
glliuy();
hutao();
queryUserInfo();
